/* for XMLPlatformUtils::Initialize() and Terminate() */
#include <xercesc/util/PlatformUtils.hpp>

/* for dom common */
#include <xercesc/dom/DOM.hpp>

/* for */
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/deprecated/DOMParser.hpp>
/* for parser */
#include <xercesc/parsers/XercesDOMParser.hpp>

/* for XMLException */
#include <xercesc/util/OutOfMemoryException.hpp>

#include <xercesc/framework/psvi/XSNamedMap.hpp>
#include <xercesc/framework/psvi/XSModel.hpp>
#include <xercesc/validators/schema/SchemaValidator.hpp>

#include <iostream>

#include "handle_opts.h"
#include "argument.h"
#include "DOMTreeErrorReporter.hpp"

using namespace std;
XERCES_CPP_NAMESPACE_USE;


int main( int argc , char ** argv ){
	int i;
	handle_opts( argc , argv , opts , opts_count );
	for( i=0 ; i<opts_count ; i++){
		print_args(opts[i]);
	}
	char *input_file = opts[INPUT_FILE].ret_value.content.content_string;

	/* Initialize the environment */
	try{
		XMLPlatformUtils::Initialize();
	}catch( const XMLException & exception ){

		cerr	<< "Error during Xerces-c Initialization.\n"
			<<" Exception message:"
			<< XMLString::transcode(exception.getMessage()) 
			<< endl;

		return exception.getCode();
	}

	/* Create a parser */
	XercesDOMParser * parser = new XercesDOMParser;

	/* Ignore the whitespace */
	parser->setIncludeIgnorableWhitespace(false);

//	parser->setValidationSchemaFullChecking(true);
//	parser->setCreateEntityReferenceNodes(true);
	parser->setValidationScheme(XercesDOMParser::Val_Always);
	parser->setDoNamespaces(true);
	parser->setDoSchema(true);   
	DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
	parser->setErrorHandler(errReporter);
	
/********** Do something at here ***********/	
	try{
		parser->parse( input_file );
	}catch( const OutOfMemoryException& ){
		cerr<<"parser failed !!!!"<<endl;
		return -1;
	}catch( const XMLException & exception ){
		cerr<<"XMLException"<<endl;
		return -2;
	}catch( const DOMException & exception ){
		cerr<<"DOMException"<<endl;
		return -3;
	}catch( ... ){
		cerr<<"Unknown Exception"<<endl;
		return -4;
	}

	try{
/*		XMLCh tempStr[100];
		XMLString::transcode("LS" , tempStr , 99);
		DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
*/

		DOMDocument *doc = parser->getDocument();
		DOMElement * element = doc->getDocumentElement();
		DOMNodeList * children = element->getChildNodes();
		cout<<"children number:"<< children->getLength()<<endl;

		children = element->getElementsByTagName(XMLString::transcode("lui:line"));
		int len = children->getLength();
		for(i=0;i<len;i++){
			printf("\n children size = %d\nname=%s\ncontent=%s string=%s\n", len , XMLString::transcode(children->item(i)->getNodeName()), XMLString::transcode(children->item(i)->getNodeValue()) , XMLString::transcode(children->item(i)->getTextContent()));
			children->item(i)->setTextContent(XMLString::transcode("1234567u"));
		}
		for(i=0;i<len;i++){
			printf("\n children size = %d\nname=%s\ncontent=%s string=%s\n", len , XMLString::transcode(children->item(i)->getNodeName()), XMLString::transcode(children->item(i)->getNodeValue()) , XMLString::transcode(children->item(i)->getTextContent()));
		}


#if 0
		DOMNodeIterator * iter;
		iter = doc->createNodeIterator( doc , DOMNodeFilter::SHOW_ALL|DOMNodeFilter::SHOW_ATTRIBUTE , NULL , true );

		DOMNode * current_p;

		for( current_p = iter->nextNode() ; current_p != 0 ; current_p = iter->nextNode() ){
			cout << XMLString::transcode(current_p->getNodeName()) << endl;
			if(current_p->getNodeValue())
				cout << "<content:\t" <<XMLString::transcode(current_p->getNodeValue())<<"endl>"<<endl;
/*			if(DOMNode::TEXT_NODE == current_p->getNodeType()){
				cout << "\t" <<XMLString::transcode(current_p->getNodeValue())<<endl;
			}

			if( current_p->hasAttributes()){
				DOMNamedNodeMap * mynodemap = current_p->getAttributes();
				XMLSize_t attrib_count = mynodemap->getLength();
				XMLSize_t i;

				for( i=0 ; i < attrib_count ; i++ ){
					DOMNode * node_p = mynodemap->item(i);

					cout 	<< "\t\t" 
						<< XMLString::transcode( node_p->getNodeName() )
						<< "="
						<< XMLString::transcode( node_p->getNodeValue() )
						<< endl;
				}
			}else{
				cout << "\tno attributes" << endl;
			}
*/
		}
#endif
	}catch(...){
	}
	cout << "*****" <<endl;

/*	PSVIElement * rootPSVI = (PSVIElement *) root;

	XSModel * schema = rootPSVI->getSchemaInformation();

	XSNamedMap<XSObject> * elementDeclaration = schema->getComponents(XSConstants::ELEMENT_DECLARATION);
elementDeclaration->getLength();
	printf( "$$$$$$$$$$$$$$ \n"  );	
	parser->getValidator().validateElement(root);
	doc->
*/

	/********** /Do somethine at here **********/

	/* Clean up the environment */
	XMLPlatformUtils::Terminate();
	return 0;
}

